﻿using DDS.IoT.Com;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace DDS.IoT.Mqtt
{
    public class MqttHostCom : ComBase
    {
        public override string Type => "mqttHostCom";

        public override string Name => "Mqtt主机";
        private MqttHostService hostService;

        public override bool Init(int port, int baudRate = 9600, string extendData = null)
        {
            this.Port = port;
            hostService = new MqttHostService();
            hostService.PushId = this.Id;
            hostService.StartAsync(extendData, OnPushData);
            Console.WriteLine("初始化MqttHostCom驱动程序成功！");
            return true;
        }
        public override event PushData OnPushData;

        public override bool Open()
        {
            var b = false;
            try
            {
                b = true;
                IsOpen = true;
            }
            catch (Exception ex)
            {

                string msg = string.Format("MqttHostCom串口打开失败：{0} ", ex.Message);
                Console.WriteLine(msg);
            }
            return b;
        }

        public override bool Close()
        {
            hostService.Dispose();
            hostService = null;
            IsOpen = false;
            OnPushData = null;
            return true;
        }

        public override string Command(string name, string data)
        {
            var outData = string.Empty;

            return outData;
        }
    }
}
